<!DOCTYPE html>
<html lang="zh-cn" >
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      name="author"
      content="awesome404"
    />
      
    <title>Webpack初体验 ｜ 鹊木笔记</title>
    
    
    
    <meta name="description" content="基本使用 一. 初始化配置 使用命令 npm init -y 初始化 package.json 输入 npm i webpack webpack-cli -D 指令安装 webpack 二. 编译打包应用 创建文件 运行指令 开发环境指令： webpack src/js/index.js -o build/js/built.js &amp;ndash;mode=development 功能：webpack 能够编译打包 js 和 json 文件，并且能将 es6 的模块化语法转换成浏览" />
      
    <meta
      name="keywords"
      content="Blog, 程序员, 前端"
    />
      <link
    rel="shortcut icon" href="https://awesome404.gitee.io/images/favicon.ico" />

    <link
      rel="stylesheet"
      type="text/css"
      media="screen"
      href="https://awesome404.gitee.io/css/normalize.css"
    />
    <link
      rel="stylesheet"
      type="text/css"
      media="screen"
      href="https://cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css"
    />
    <link rel="stylesheet" type="text/css" media="screen" href="https://awesome404.gitee.io/css/zozo.css" />
    <link
      rel="stylesheet"
      type="text/css"
      media="screen"
      href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css"
    />
    <link rel="stylesheet" type="text/css" media="screen" href="https://awesome404.gitee.io/css/highlight.css" />

    
    
  </head>
</html>


<body>
    <div class="main animate__animated animate__fadeInDown">
        
<div class="nav_container animated fadeInDown">
  <div class="site_nav" id="site_nav">
    <ul>
      
      <li>
        <a href="/">首页</a>
      </li>
      
      <li>
        <a href="/posts/">归档</a>
      </li>
      
      <li>
        <a href="/tags/">标签</a>
      </li>
      
      <li>
        <a href="/about/">关于</a>
      </li>
      
    </ul>
  </div>
  <div class="menu_icon">
    <a id="menu_icon">
      <i class="ri-settings-line"></i>
    </a>
  </div>
</div>

        <div class="header animated fadeInDown">
  <div class="site_title_container">
    <div class="site_title">
      <h1>
        <a href="https://awesome404.gitee.io">
          <span>鹊木笔记</span>
        </a>
      </h1>
    </div>
    <div class="description">
      <p class="sub_title">
        前端菜鸡的自我修养
      </p>
      <div class="my_socials">
         
        <a href="https://github.com/404Awesome" title="github" target="_blank"
          ><i class="ri-github-fill"></i
        ></a>
         
        <a
          href="https://awesome404.gitee.io/index.xml"
          type="application/rss+xml"
          title="rss"
          target="_blank"
          ><i class="ri-rss-fill"></i
        ></a>
      </div>
    </div>
  </div>
</div>

        <div class="content">
            <div class="post_page">
                <div class="post animate__animated animate__fadeInDown">
                    <div class="post_title post_detail_title">
                        <h2><a href='/posts/webpack%E5%88%9D%E4%BD%93%E9%AA%8C/'>Webpack初体验</a></h2>
                        <span class="date">2020.06.20</span>
                    </div>
                    <div class="post_content markdown"><h1 id="基本使用">基本使用</h1>
<h3 id="一-初始化配置">一. 初始化配置</h3>
<ol>
<li>
<p>使用命令 npm init -y 初始化 package.json</p>
</li>
<li>
<p>输入 npm i webpack webpack-cli -D 指令安装 webpack</p>
</li>
</ol>
<h3 id="二-编译打包应用">二. 编译打包应用</h3>
<ol>
<li>
<p>创建文件</p>
</li>
<li>
<p>运行指令</p>
<ol>
<li>
<p>开发环境指令： webpack src/js/index.js -o build/js/built.js &ndash;mode=development<br />                功能：webpack 能够编译打包 js 和 json 文件，并且能将 es6 的模块化语法转换成浏览器能识别的语法。</p>
</li>
<li>
<p>生产环境指令：webpack src/js/index.js -o build/js/built.js &ndash;mode=production<br />
功能：在开发配置功能上多一个功能，压缩代码。</p>
<blockquote>
<p>webpack 入口文件 -o 输出文件 &ndash;mode=模式<br />其中 mode 前方是两个横杠</p>
</blockquote>
</li>
</ol>
</li>
<li>
<p>结论</p>
<p>webpack 能够编译打包 js 和 json 文件
能将 es6 的模块化语法转换成浏览器能识别的语法，能压缩代码</p>
</li>
<li>
<p>问题</p>
<p>不能编译打包 css、img 等文件
不能将 js 的 es6 基本语法转化为 es5 以下语法</p>
</li>
</ol>
<h3 id="三-目录结构">三. 目录结构</h3>
<img src='/img/post/webpack/dir.png' />
<blockquote>
<p>使用 Nodejs 的语法输出打包后的 js 文件，输出结果如上图</p>
</blockquote>
<h1 id="开发环境的基本配置">开发环境的基本配置</h1>
<h3 id="一-创建配置文件">一. 创建配置文件</h3>
<ol>
<li>
<p>在项目根目录下创建一个文件名为:  webpack.config.js</p>
</li>
<li>
<p>配置文件内容如下</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="p">{</span> <span class="nx">resolve</span> <span class="p">}</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;path&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">entry</span><span class="o">:</span> <span class="s1">&#39;./src/index.js&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nx">output</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="nx">filename</span><span class="o">:</span> <span class="s1">&#39;built.js&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="nx">path</span><span class="o">:</span> <span class="nx">resolve</span><span class="p">(</span><span class="nx">__dirname</span><span class="p">,</span> <span class="s1">&#39;dist&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">},</span>
</span></span><span class="line"><span class="cl">    <span class="nx">mode</span><span class="o">:</span> <span class="s1">&#39;development&#39;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div></li>
<li>
<p>运行指令：webpack</p>
</li>
<li>
<p>结果和上方的打包方式一样</p>
</li>
</ol>
<h3 id="二-目录结构">二. 目录结构</h3>
<p>如上方图片所示</p>
<h3 id="三-配置文件">三. 配置文件</h3>
<ol>
<li>webpack.config.js 配置文件中使用 Commonjs 规范，module.exports 导出一个对象，所有的配置信息在这个对象中定义</li>
<li>其中 entry 是入口文件</li>
<li>output 输出文件是对象形式，output.filename 是输出文件的名称，output.path 是输出路径，这个路径不能是相对路径，应该是一个绝对路径，所以使用 node 中的变量 path 中的 resolve 方式</li>
<li>mode 是模式，有两种值 development 开发模式和 production 生产模式</li>
</ol>
</div>
                    <div class="post_footer">
                        
                        
                        <div id="link">
                            <p>本博客所有文章除特别声明外，均采用 CC BY-SA 3.0协议 。转载请注明出处！</p>
                            <p><span id="link_href"></span><i class="ri-file-copy-2-line link_btn"></i></p>
                        </div>
                        
                        <div class="meta">
                            <div class="info">
                                <span class="field tags">
                                    <i class="ri-price-tag-2-line"></i>
                                    
                                    <a href="https://awesome404.gitee.io/tags/webpack/">WebPack</a>
                                    
                                </span>
                            </div>
                        </div>
                        
                    </div>
                </div>
                
                
            </div>
        </div>
    </div>
    <a id="back_to_top" href="#" class="back_to_top"><i class="ri-arrow-up-s-line"></i></a>
    
<style>
  #hitokoto {
    overflow: hidden;
    margin: 0 auto;
    width: 80%;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 16px;
    color: #424242;
  }

  .footer {
    padding: 30px 0px;
  }
</style>

<footer class="footer">
  <p id="hitokoto">:D 获取中...</p>
</footer>

<script>
  fetch("https://v1.hitokoto.cn")
    .then((response) => response.json())
    .then((data) => {
      const hitokoto = document.getElementById("hitokoto");
      hitokoto.href = "https://hitokoto.cn/?uuid=" + data.uuid;
      hitokoto.innerText = data.hitokoto;
    })
    .catch((err) => err);
</script>

    <script src="https://awesome404.gitee.io/js/jquery-3.5.1.min.js"></script>
<link href="https://awesome404.gitee.io/css/fancybox.min.css" rel="stylesheet">
<script src="https://awesome404.gitee.io/js/fancybox.min.js"></script>
<script src="https://awesome404.gitee.io/js/zozo.js"></script>




</body>


<script>
    (function (){
        
        function handleIcon (remove, target){
            this.classList.remove(remove);
            this.classList.add(target);
        }

        let hrefEl = document.getElementById("link_href");
        let copyBtn = document.getElementsByClassName("link_btn")[0];
        if (hrefEl){
            hrefEl.innerText = `文章链接: ${location.href}`;
            
            copyBtn.onclick = (event) => {
                
                navigator.clipboard.writeText(`文章转载自: ${location.href}`)
                
                handleIcon.call(event.target, "ri-file-copy-2-line", "ri-check-line");
                let timer = setTimeout(() => {
                    handleIcon.call(event.target, "ri-check-line", "ri-file-copy-2-line");
                    clearTimeout(timer)
                }, 1000)
            }
        }
    })();
</script>
</html>